// 创建无副作用的actions和对应的reducer
// 针对单纯更改值的动作

const createNoEffect = actions => {
  const actionsMap = {};
  const reducers = {};
  actions.forEach(item => {
    actionsMap[item] = value => value;
    const key = item
      .split('_')
      .filter(i => i)
      .slice(1)
      .map((i, index) => {
        if (index) {
          return i.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase());
        } else {
          return i.toLowerCase();
        }
      })
      .join('');

    reducers[item] = (state, action) => {
      const { error, payload } = action;
      if (!error) {
        return {
          ...state,
          [key]: payload
        };
      } else {
        return state;
      }
    };
  });
  return {
    actions: actionsMap,
    reducers
  };
};

export default createNoEffect;
